home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / library / __load_seg.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  10KB  |  403 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __load_seg.c,v 1.1.1.1 1994/04/04 04:30:54 amiga Exp
  20.  *
  21.  *  __load_seg.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:54  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.5  1992/09/14  01:38:35  mwild
  26.  *  fix a bug with #! expansion (forgot separating /)
  27.  *
  28.  *  Revision 1.4  1992/08/09  20:37:07  amiga
  29.  *  change to use 2.x header files by default
  30.  *
  31.  *  Revision 1.3  1992/07/04  19:02:39  mwild
  32.  *  fix typo, the buffer for interpreter-expansion was ways too small...
  33.  *
  34.  * Revision 1.2  1992/05/22  01:45:00  mwild
  35.  * rewrote interpreter expansion, `should' now work as expected
  36.  *
  37.  * Revision 1.1  1992/05/14  19:55:40  mwild
  38.  * Initial revision
  39.  *
  40.  */
  41.  
  42. #define KERNEL
  43. #include "ixemul.h"
  44. #include "kprintf.h"
  45.  
  46. #include <ctype.h>
  47. #include <string.h>
  48.  
  49. /* 2.0 support */
  50. #include <utility/tagitem.h>
  51. #include <dos/dostags.h>
  52.  
  53. extern void *kmalloc (size_t size);
  54.  
  55. struct my_seg {
  56.   BPTR    segment;    /* the thing our clients can use */
  57.   enum { LOADSEG, RESSEG } type;
  58.   u_int    priv;        /* information depending on type */
  59. };
  60.  
  61.  
  62. static struct my_seg *try_load_seg (BPTR lock, char *name, char **args);
  63.  
  64.  
  65. static inline struct my_seg *
  66. check_resident (char *tmp)
  67. {
  68.   struct my_seg *res = 0;
  69.   struct Segment *seg = 0;
  70.  
  71.       /* big problem: Commo only stores the bare names in the resident
  72.          list. So we have to truncate to the filename part, and so lose
  73.          the ability to explicitly load the disk version even if a 
  74.          resident version is installed */
  75.  
  76.       char *cp = rindex (tmp, '/');
  77.       if (cp)
  78.     tmp = cp + 1;
  79.       else if ((cp = index (tmp, ':')))
  80.     tmp = cp + 1;
  81.  
  82.       Forbid ();
  83.       seg = FindSegment (tmp, 0, 0);
  84.       if (seg)
  85.         {
  86.           /* strange they didn't provide a function for this... */
  87.           if (seg->seg_UC >= 0) 
  88.         seg->seg_UC++;
  89.     }
  90.       Permit ();
  91.  
  92.   if (seg && (res = (struct my_seg *) kmalloc (sizeof (*res))))
  93.     {
  94.       res->segment = seg->seg_Seg;
  95.       res->type    = RESSEG;
  96.       res->priv    = (u_int) seg;
  97.     }
  98.   else if (seg)
  99.     {
  100.       Forbid ();
  101.       if (seg->seg_UC > 0)
  102.     seg->seg_UC--;
  103.       Permit ();
  104.     }
  105.  
  106.   return res;
  107. }
  108.  
  109.  
  110. static inline struct my_seg *
  111. check_loadseg (char *tmp)
  112. {
  113.   struct my_seg *res = 0;
  114.   BPTR seg;
  115.   int err;
  116.  
  117.   /* Note that LoadSeg returns NULL and sets IoErr() to 0 if the file it
  118.      is trying to load is less than 4 bytes long. */
  119.   seg = LoadSeg (tmp);
  120.   if (seg && (res = kmalloc (sizeof (*res))))
  121.     {
  122.       res->segment = seg;
  123.       res->type       = LOADSEG;
  124.       res->priv    = seg;
  125.     }
  126.   else if (seg)
  127.     {
  128.       UnLoadSeg (seg);
  129.       errno = ENOMEM;
  130.       return NULL;
  131.     }
  132.   err = IoErr();
  133.   errno = (err ? __ioerr_to_errno(err) : ENOENT);
  134.  
  135.   if (errno == EINVAL)
  136.     errno = ENOENT;
  137.     
  138.   return res;
  139. }
  140.  
  141.  
  142. void
  143. __free_seg (BPTR *seg)
  144. {
  145.   struct my_seg *ms;
  146.   
  147.   ms = (struct my_seg *) seg;
  148.   
  149.   if (ms->type == RESSEG)
  150.     {
  151.       struct Segment *s = (struct Segment *) ms->priv;
  152.  
  153.       Forbid ();
  154.       if (s->seg_UC > 0)
  155.     s->seg_UC--;
  156.       Permit ();
  157.     }
  158.   else
  159.     UnLoadSeg (ms->priv);
  160.  
  161.   kfree (ms);
  162. }
  163.  
  164.  
  165. /*
  166.  * This function does what LoadSeg() does, and a little bit more ;-)
  167.  * Besides walking the PATH of the user, we try to do interpreter expansion as
  168.  * well. But, well, we do it a little bit different then a usual Amiga-shell.
  169.  * We check the magic cookies `#!' and `;!', and if found, run the interpreter
  170.  * specified on this first line of text. This does *not* depend on any script
  171.  * bit set!
  172.  */
  173.  
  174. /*
  175.  * IMPORTANT: only call this function with all signals masked!!! 
  176.  */
  177.  
  178. /*
  179.  * name:        the name of the command to load. Can be relative to installed PATH
  180.  * args:        if set, a string to the first part of an expanded command is stored
  181.  */
  182.  
  183. BPTR *
  184. __load_seg (char *name, char **args)
  185. {
  186.   BPTR lock;
  187.   struct my_seg *seg;
  188.  
  189.   /* perhaps the name is vanilla enough, so that even LoadSeg() groks it? */
  190.   if (args) *args = 0;
  191.  
  192.   seg = check_resident (name);
  193.  
  194.   if (! seg)
  195.     seg = check_loadseg (name);
  196.  
  197.   if (seg)
  198.     return &seg->segment;
  199.  
  200.   if (errno != ENOENT)
  201.     return 0;
  202.  
  203.   /* try to lock the file (using __lock() provides full path-parsing ;-)) */
  204.   lock = __lock (name, ACCESS_READ);
  205.   if (lock)
  206.     {
  207.       int err;
  208.  
  209.       /* this is tricky.. it is legal to CurrentDir() to a file. This is what
  210.        * we do here, we try to LoadSeg("") afterwards ;-)) */
  211.       seg = try_load_seg (lock, "", args);
  212.       err = errno;
  213.  
  214.       __unlock (lock);
  215.  
  216.       errno = err;
  217.       if (!seg && errno != ENOENT)
  218.         return 0;
  219.     }
  220.  
  221.   /* now we may have a valid segment */
  222.   if (seg)
  223.     return &seg->segment;
  224.  
  225.   /* if the command was specified with some kind of path, for example with a
  226.    * device or a directory in it, we don't run it thru the PATH expander
  227.    */
  228.   if (strpbrk (name, ":/"))
  229.     {
  230.       errno = ENOENT;
  231.       return 0;
  232.     }
  233.  
  234.   /* so the command is not directly addressable, but perhaps it's in our PATH? */
  235.   {
  236.     struct Process *me = (struct Process *)(SysBase->ThisTask);
  237.     struct CommandLineInterface *cli;
  238.  
  239.     /* but we need a valid CLI then */
  240.     if ((cli = BTOCPTR (me->pr_CLI)))
  241.       {
  242.     struct path_element {
  243.       BPTR    next;
  244.       BPTR     lock;
  245.     } *lock_list;
  246.  
  247.     for (lock_list = BTOCPTR (cli->cli_CommandDir);
  248.          lock_list;
  249.          lock_list = BTOCPTR (lock_list->next))
  250.       {
  251.         if ((seg = try_load_seg (lock_list->lock, name, args)))
  252.           break;
  253.         if (errno != ENOENT)
  254.               return 0;
  255.       }
  256.       }
  257.   }
  258.   
  259.   if (seg)
  260.     return &seg->segment;
  261.  
  262.   errno = ENOENT;
  263.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  264.   return 0;
  265. }
  266.  
  267. static struct my_seg *
  268. try_load_seg (BPTR lock, char *name, char **args)
  269. {
  270.   BPTR ocd;
  271.   struct my_seg *seg;
  272.  
  273.   if (args) *args = 0;
  274.   
  275.   ocd = CurrentDir (lock);
  276.   
  277.   seg = check_loadseg (name);
  278.  
  279.   if (!seg && errno != ENOENT)
  280.     return 0;
  281.  
  282.   /* try to do interpreter - expansion, but only if args is non-zero */
  283.   if (!seg && args)
  284.     {
  285.       int fd, n;
  286.       char magic[5];
  287.       struct stat stb;
  288.       
  289.       if (syscall (SYS_stat, name, &stb) == 0 && S_ISREG (stb.st_mode))
  290.     {
  291.       if ((fd = syscall (SYS_open, name, 0)) >= 0)
  292.             {
  293.               /*
  294.                *  If the .key line of an AmigaDOS script isn't the first
  295.                *  line of the script, the AmigaDOS shell gets very confused.
  296.                *  Therefore, we skip the first line if it begins with .key,
  297.                *  and we test the second line for #! or ;!.
  298.                */
  299.           if ((n = syscall (SYS_read, fd, magic, 4)) == 4)
  300.             {
  301.               magic[4] = 0;
  302.               if (!strcasecmp(magic, ".key"))
  303.                 {
  304.                   n = 0;
  305.                   /* skip this line */
  306.                   while (syscall (SYS_read, fd, magic, 1) == 1)
  307.                     if (magic[0] == '\n')
  308.                       {
  309.                         n = syscall (SYS_read, fd, magic, 4);
  310.                         break;
  311.                       }
  312.                 }
  313.             }
  314.           if (n >= 2 && (magic[0] == '#' || magic[0] == ';') && magic[1] == '!')
  315.             {
  316.               char interp[MAXPATHLEN + 1], *interp_start;
  317.           
  318.               interp[0] = magic[2];
  319.               interp[1] = magic[3];
  320.               n -= 2;
  321.               n = n + syscall (SYS_read, fd, interp + n, MAXINTERP - n);
  322.               if (n > 0)
  323.             {
  324.               char *cp, ch;
  325.               char *interp_path;
  326.  
  327.               /* okay.. got one.. terminate with 0 and try to find end of it */
  328.               interp[n] = 0;
  329.               for (interp_start = interp; isspace(*interp_start) && interp_start < interp + n; interp_start++);
  330.               for (cp = interp_start; cp < interp + n; cp++)
  331.                 if (*cp == 0 || isspace (*cp)) break;
  332.               ch = *cp;
  333.               *cp = 0;
  334.  
  335.               /* okay, lets try to load this instead. Call us recursively,
  336.                * but leave out the argument-argument, so we can't get
  337.                * into infinite recursion. Interpreter-Expansion is only
  338.                * done the first time __load_seg() is called from
  339.                * execve()
  340.                */
  341.               seg = (struct my_seg *) __load_seg (interp_start, 0);
  342.               *cp = ch;
  343.               if (!seg)
  344.                 goto ret;
  345.           
  346.               /* in this case, set the argument as well. 
  347.                */
  348.  
  349.               /* first skip intergap whitespace */
  350.               for (; cp < interp + n; cp++)
  351.             if (!*cp || ! isspace (*cp) || *cp == '\n')
  352.               break;
  353.  
  354.               if (*cp && *cp != '\n')
  355.                 {
  356.               /* we read a certain amount of bytes, but we 
  357.                * unconditionally stop when we hit newline
  358.                */
  359.               interp_path = cp;
  360.  
  361.                   /* crop any further arguments, only ONE argument
  362.                    * is supported
  363.                */
  364.                   for (; cp < interp + n; cp++) 
  365.                 if (isspace (*cp)) 
  366.                   break;
  367.                   if (cp < interp + n)
  368.                 *cp = 0;
  369.  
  370.               *cp++ = ' ';
  371.             }
  372.               else
  373.             cp = interp_path = interp_start;
  374.  
  375.               if (name[0] != '/' && !index (name, ':'))
  376.             {
  377.               if (NameFromLock (lock, cp, 
  378.                         MAXPATHLEN-(cp-interp)) == -1)
  379.                             {
  380.                               if (*name)
  381.                     strcat (strcat (cp, "/"), name);
  382.                 }
  383.               else
  384.                 strcpy (cp, name);
  385.             }
  386.               else
  387.             strcpy (cp, name);
  388.  
  389.               *args = (char *) syscall (SYS_strdup, interp_path);
  390.             }
  391.             }
  392.           syscall (SYS_close, fd);
  393.         }
  394.         }
  395.     }
  396. ret:  
  397.   CurrentDir (ocd);
  398.  
  399.   if (!seg)  
  400.     errno = ENOENT;
  401.   return seg;
  402. }
  403.